<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sys_job', function (Blueprint $table) {
            $table->comment('定时任务调度表');
            $table->bigInteger('job_id', false)->comment('任务ID');
            $table->string('job_name', 64)->default('')->comment('任务名称');
            $table->string('job_group', 64)->default('DEFAULT')->comment('任务组名');
            $table->string('invoke_target', 500)->comment('调用目标字符串');
            $table->string('cron_expression')->nullable()->default('')->comment('cron执行表达式');
            $table->string('misfire_policy', 20)->nullable()->default('3')->comment('计划执行错误策略（1立即执行 2执行一次 3放弃执行）');
            $table->char('concurrent', 1)->nullable()->default('1')->comment('是否并发执行（0允许 1禁止）');
            $table->char('status', 1)->nullable()->default('0')->comment('状态（0正常 1暂停）');
            $table->string('create_by', 64)->nullable()->default('')->comment('创建者');
            $table->dateTime('create_time')->nullable()->comment('创建时间');
            $table->string('update_by', 64)->nullable()->default('')->comment('更新者');
            $table->dateTime('update_time')->nullable()->comment('更新时间');
            $table->string('remark', 500)->nullable()->default('')->comment('备注信息');

            $table->primary(['job_id', 'job_name', 'job_group']);
        });
        Schema::table('sys_job', fn (Blueprint $table) => $table->bigInteger('job_id', true)->change());
        Schema::create('sys_job_log', function (Blueprint $table) {
            $table->comment('定时任务调度日志表');
            $table->bigInteger('job_log_id', true)->comment('任务日志ID');
            $table->string('job_name', 64)->comment('任务名称');
            $table->string('job_group', 64)->comment('任务组名');
            $table->string('invoke_target', 500)->comment('调用目标字符串');
            $table->string('job_message', 500)->nullable()->comment('日志信息');
            $table->char('status', 1)->nullable()->default('0')->comment('执行状态（0正常 1失败）');
            $table->string('exception_info', 2000)->nullable()->default('')->comment('异常信息');
            $table->dateTime('create_time')->nullable()->comment('创建时间');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sys_job');
        Schema::dropIfExists('sys_job_log');
    }
};
